﻿module Sustainalytics.EsgRatings {

    import Company = Sustainalytics.Company;

    export class OverallPerformanceController {

        public esgOverallPerformanceSummary: EsgOverallPerformanceSummaryDto;
        public esgOverallPerformanceSummary_relative_esgPositionName: string;
        public esgOverallPerformanceSummary_percentile_comparativeGroupPercentileRank_ordinalName: string;
        public esgOverallPerformanceSummary_percentile_esgPositionName: string;
        public esgOverallPerformanceSummary_environment_esgPositionName: string;
        public esgOverallPerformanceSummary_social_esgPositionName: string;
        public esgOverallPerformanceSummary_governance_esgPositionName: string;

        private userId: string;
        private esgProfileId: string;

        constructor(
            private $scope: Company.ICompanyBasedScope,
            private $q: ng.IQService,
            private $timeout: ng.ITimeoutService,
            private $filter: ng.IFilterService,
            private EsgOverallPerformanceSummaryService: IEsgOverallPerformanceSummaryService,
            private NumberUtilityService: Common.NumberUtilityService,
            private $rootScope) {

            var ctx = this;

            if (ctx.$rootScope['userInfo'] == null) {
                ctx.$rootScope['userInfo'] = angular.fromJson(localStorage.getItem('sust_userInfo'));
            }
            ctx.userId = ctx.$rootScope['userInfo'].userId;
            ctx.esgProfileId = ctx.$rootScope['userInfo'].esgProfileId;

            ctx.init();
        }

        private init(): ng.IPromise<any> {
            var ctx = this;

            var promiseOverall = ctx.getEsgOverallPerformanceSummary();

            return promiseOverall;
        }

        private getEsgOverallPerformanceSummary(): ng.IPromise<EsgOverallPerformanceSummaryDto> {
            var ctx = this;
            var promise = ctx.EsgOverallPerformanceSummaryService.get(ctx.$scope.currentCompany.id, ctx.esgProfileId);

            promise.then(function (res) {
                ctx.$timeout(function () {
                    ctx.$scope.$apply(function () {
                        ctx.esgOverallPerformanceSummary = res;
                        ctx.esgOverallPerformanceSummary_relative_esgPositionName = ctx.esgOverallPerformanceSummary.relative.esgPosition.name;
                        ctx.esgOverallPerformanceSummary_percentile_comparativeGroupPercentileRank_ordinalName = ctx.NumberUtilityService.getOrdinalNameEnd(ctx.esgOverallPerformanceSummary.percentile.comparativeGroupPercentileRank);
                        ctx.esgOverallPerformanceSummary_percentile_esgPositionName = ctx.esgOverallPerformanceSummary.percentile.esgPosition.name;
                        ctx.esgOverallPerformanceSummary_environment_esgPositionName = ctx.esgOverallPerformanceSummary.environment.esgPosition && ctx.esgOverallPerformanceSummary.environment.esgPosition.name;
                        ctx.esgOverallPerformanceSummary_social_esgPositionName = ctx.esgOverallPerformanceSummary.social.esgPosition && ctx.esgOverallPerformanceSummary.social.esgPosition.name;
                        ctx.esgOverallPerformanceSummary_governance_esgPositionName = ctx.esgOverallPerformanceSummary.governance.esgPosition && ctx.esgOverallPerformanceSummary.governance.esgPosition.name;
                    });
                });
            },() => {

                    ga('create', 'UA-4379897-6', {
                        'userId': ctx.userId, 'cookieDomain': 'none', 'name': 'globalAccess'
                    });

                    ga('globalAccess.send', 'exception', { 'exDescription': 'Esg Ratings: Overall Performance Controller -> Error loading Overall Performance Summary' });
                });

            return promise;
        }

        public getEsgPosition(): any {
            return EsgPositionEnum;
        }
    }


    export var overallPerformanceControllerName = 'OverallPerformanceController';

    angular.module(esgRatingsModuleName).controller(overallPerformanceControllerName,
        ['$scope', '$q', '$timeout', '$filter', 'EsgOverallPerformanceSummaryService', 'NumberUtilityService', '$rootScope', OverallPerformanceController]);
}